package com.yt.base.filter;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.yt.base.service.AclUserOperationService;
import com.yt.base.util.Constants;
import com.yt.base.vo.UserMenuVo;

public class AuthFilter implements HandlerInterceptor {

	private String whiteList;

	public String getWhiteList() {
		return whiteList;
	}

	public void setWhiteList(String whiteList) {
		this.whiteList = whiteList;
	}

	@Autowired
	private AclUserOperationService userOperationService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		StringBuffer url = request.getRequestURL();
		if (url != null && !url.toString().endsWith("code")) {
			String context = getContextPath(request);
			if (whiteList.contains(context + ";"))
				return true;
			HttpSession session = request.getSession();
			Object loginObj = session.getAttribute(Constants.LOGINUSER);
			if (loginObj != null) {
				Object opObj = session.getAttribute(Constants.USER_OP_KEY);
				if (opObj != null) {
					List<String> opList = (List<String>) opObj;

					// QueryFilter filter = new DynamicSQLFilterImpl();
					// filter.setPagingBean(null);
					// List<AclUserOperation> userOperationList =
					// userOperationService.find(filter);
				} else {
					response.sendRedirect(request.getContextPath() + "/login.html");
					return false;
				}
			} else {
				response.sendRedirect(request.getContextPath() + "/login.html");
				return false;
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (modelAndView == null)
			return;

		String start = request.getParameter("start");
		if (start != null && !"".equals(start)) {
			modelAndView.addObject("start", start);
		} else {
			modelAndView.addObject("start", 1);
		}
		String limit = request.getParameter("limit");
		if (limit != null && !"".equals(limit)) {
			modelAndView.addObject("limit", limit);
		} else {
			modelAndView.addObject("limit", 25);
		}

		String type = request.getParameter("type");
		if (type != null && !"".equals(type)) {
			modelAndView.addObject("type", type);
		} else {
			UserMenuVo vo = (UserMenuVo) request.getSession().getAttribute(Constants.USER_MENU_KEY);
			if (!vo.getCategoryList().isEmpty()) {
				modelAndView.addObject("type", 0);
			}
		}
		String itemId = request.getParameter("itemId");
		if (itemId != null && !"".equals(itemId)) {
			modelAndView.addObject("itemId", request.getParameter("itemId"));
		}
	}

	/**
	 * 获取请求的相对的url
	 * 
	 * @param request
	 * @return
	 */
	private String getContextPath(HttpServletRequest request) {
		String uri = request.getServletPath();
		String pathInfo = request.getPathInfo();
		if (pathInfo != null && pathInfo.length() > 0) {
			uri = uri + pathInfo;
		}
		return uri;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
